package com.example.fresh;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.KeepAccounts.Fragment_keepacounts;
import com.example.hobby.Fragment_hobby;

public class MainActivityForHobby extends AppCompatActivity implements View.OnClickListener{
    private LinearLayout tabHome;
    private LinearLayout tabMain;
    private LinearLayout tabHobby;

    private ImageButton buttonHome;
    private ImageButton buttonMain;
    private ImageButton buttonHobby;
//
    private Fragment fragmentMain = new Fragment_hobby();
    private Fragment fragmentHobby = new Fragment_keepacounts();

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main_for_hobby);

        initView();
        initFragment();
        initEvent();
        selectFragment(1);

    }

    private void initFragment()
    {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.id_content, fragmentHobby);
        transaction.add(R.id.id_content, fragmentMain);
        transaction.commit();
    }

    public void initView()
    {
        tabHobby = findViewById(R.id.id_tab_hobby);
        tabHome = findViewById(R.id.id_tab_home);
        tabMain = findViewById(R.id.id_tab_main);

        buttonHobby =findViewById(R.id.hobbyButton);
        buttonHome = findViewById(R.id.homeButton);
        buttonMain = findViewById(R.id.mainButton);
    }

    public void hideFragment(FragmentTransaction transaction)
    {
        transaction.hide(fragmentHobby);
        transaction.hide(fragmentMain);
    }

    public void selectFragment(int i)
    {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        resetImg();
        switch (i) {
            case 0:
                transaction.show(fragmentHobby);
                buttonHobby.setImageResource(R.drawable.bottom_forhobby_1);
                break;
            case 1:
                transaction.show(fragmentMain);
                buttonMain.setImageResource(R.drawable.bottom_forhobby_2);
                break;
            case 2:
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                buttonHome.setImageResource(R.drawable.bottom_forhobby_3);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void resetImg() {
        buttonHobby.setImageResource(R.drawable.bottom_forhobby_1_press);
        buttonHome.setImageResource(R.drawable.bottom_forhobby_3_press);
        buttonMain.setImageResource(R.drawable.bottom_forhobby_2_press);
    }

    private void initEvent() {
        tabHobby.setOnClickListener(this);
        tabMain.setOnClickListener(this);
        tabHome.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        resetImg();
        switch (view.getId()) {
            case R.id.id_tab_hobby:
                selectFragment(0);
                break;
            case R.id.id_tab_main:
                selectFragment(1);
                break;
            case R.id.id_tab_home:
                selectFragment(2);
                break;
            default:
                break;

        }
    }
}
